const pubSub = {
	id: 1,
	callbacks: {}
};
pubSub.subscribe = function (channel, callback) {
	let token = "token_" + this.id++;
	if (this.callbacks[channel]) {
		this.callbacks[channel][token] = callback;
	} else {
		this.callbacks[channel] = {
			[token]: callback
		}
	}

	return token;
}
pubSub.publish = function (channel, data) {
	if (this.callbacks[channel]) {
		Object.values(this.callbacks[channel]).forEach(callback => {
			callback(data);
		})
	}
}
pubSub.unsubscribe = function (flag) {
	if (flag === undefined) {
		this.callbacks = {};
	} else if (typeof flag === 'string') {
		if (flag.indexOf('token_') === 0) {
			let callbackObj = Object.values(this.callbacks).find(obj => obj.hasOwnProperty(flag));
			if (callbackObj) {
				delete callbackObj[flag];
			}
		}else {
			delete this.callbacks[flag]
		}
	}
}
